/* eslint no-nested-ternary: 0,no-restricted-properties:0 */
import { fabric } from 'fabric';

export const ErasedGroup = fabric.util.createClass(fabric.Group, {
  original: null,
  erasedPath: null,
  initialize(original, erasedPath, options, isAlreadyGrouped) {
    this.original = original;
    this.erasedPath = erasedPath;
    this.callSuper('initialize', [this.original, this.erasedPath], options, isAlreadyGrouped);
  },
  _calcBounds(onlyWidthHeight) {
    const aX = [];
    const aY = [];
    const props = ['tr', 'br', 'bl', 'tl'];
    const jLen = props.length;
    const ignoreZoom = true;

    const o = this.original;
    o.setCoords(ignoreZoom);
    for (let j = 0; j < jLen; j++) {
      const prop = props[j];
      aX.push(o.oCoords[prop].x);
      aY.push(o.oCoords[prop].y);
    }

    this._getBounds(aX, aY, onlyWidthHeight);
  },
});
export const EraserBrush = fabric.util.createClass(fabric.PencilBrush, {
  _finalizeAndAddPath() {
    const ctx = this.canvas.contextTop;
    ctx.closePath();
    if (this.decimate) {
      this._points = this.decimatePoints(this._points, this.decimate);
    }
    const pathData = this.convertPointsToSVGPath(this._points).join('');
    if (pathData === 'M 0 0 Q 0 0 0 0 L 0 0') {
      this.canvas.requestRenderAll();
      return;
    }
    const path = this.createPath(pathData);
    path.globalCompositeOperation = 'destination-out';
    path.selectable = false;
    path.evented = false;
    path.absolutePositioned = true;

    const objects = this.canvas.getObjects().filter((obj) => {
      if (!obj.intersectsWithObject(path)) return false;
      return true;
    });

    if (objects.length > 0) {
      const mergedGroup = new fabric.Group(objects);
      const newPath = new ErasedGroup(mergedGroup, path);
      const { left } = newPath;
      const { top } = newPath;
      const newData = newPath.toDataURL({
        withoutTransform: true,
      });
      fabric.Image.fromURL(newData, (fabricImage) => {
        fabricImage.set({
          left,
          top,
        });
        this.canvas.remove(...objects);
        fabricImage.selectable = false;
        this.canvas.add(fabricImage);
      });
    }

    this.canvas.clearContext(this.canvas.contextTop);
    this.canvas.renderAll();
    this._resetShadow();
  },
});
